package com.edu.study.base.stuCourseExercisesResult.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.study.base.stuCourseExercisesResult.domain.CompositionPage;
import com.edu.study.base.stuCourseExercisesResult.domain.StuCourseExercisesResult;
import com.edu.study.base.stuCourseExercisesResult.mapper.StuCourseExercisesResultMapper;
import com.edu.study.base.stuCourseExercisesResult.service.IStuCourseExercisesResultService;
import com.edu.study.base.stuQuestionOption.domain.StuQuestionOption;
import com.edu.study.base.stuQuestionOption.service.IStuQuestionOptionService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课后练习结果汇总Service业务层处理
 * 
 * @author zw
 * @date 2021-08-30
 */
@Service
//@DataSource(value = DataSourceType.SLAVE)
public class StuCourseExercisesResultServiceImpl extends ServiceImpl<StuCourseExercisesResultMapper, StuCourseExercisesResult> implements IStuCourseExercisesResultService
{
    @Autowired
    private StuCourseExercisesResultMapper stuCourseExercisesResultMapper;
    @Autowired
    private IStuQuestionOptionService stuQuestionOptionService;

    /**
     * 查询课后练习结果汇总
     * 
     * @param id 课后练习结果汇总ID
     * @return 课后练习结果汇总
     */
    @Override
    public StuCourseExercisesResult selectStuCourseExercisesResultById(Long id)
    {
        return stuCourseExercisesResultMapper.selectStuCourseExercisesResultById(id);
    }

    /**
     * 查询课后练习结果汇总列表
     * 
     * @param stuCourseExercisesResult 课后练习结果汇总
     * @return 课后练习结果汇总
     */
    @Override
    public List<StuCourseExercisesResult> selectStuCourseExercisesResultList(StuCourseExercisesResult stuCourseExercisesResult)
    {
        return stuCourseExercisesResultMapper.selectStuCourseExercisesResultList(stuCourseExercisesResult);
    }

    @Override
    public List<StuCourseExercisesResult> selectTeacherTaskList(StuCourseExercisesResult stuCourseExercisesResult) {
        return stuCourseExercisesResultMapper.selectTeacherTaskList(stuCourseExercisesResult);
    }

    @Override
    public List<StuCourseExercisesResult> selectStuCourseExercisesAnswnerResult(StuCourseExercisesResult stuCourseExercisesResult) {
        List<StuCourseExercisesResult> list = stuCourseExercisesResultMapper.selectStuCourseExercisesAnswnerResult(stuCourseExercisesResult);
        List<Map<String,Object>> mapList = new ArrayList<>();

        for (StuCourseExercisesResult courseExercisesResult : list) {
            StuQuestionOption stuQuestionOption = new StuQuestionOption();
            stuQuestionOption.setQuestionLibraryId(Long.parseLong(courseExercisesResult.getQuestionId()));
            List<StuQuestionOption> optionList = stuQuestionOptionService.selectStuQuestionOptionList(stuQuestionOption);
            //courseExercisesResult.setQuestionOptions(optionList);
            for (StuQuestionOption questionOption : optionList) {
                Map<String,Object> map = new HashMap<>();
                map.put("optionSort",questionOption.getOptionContentSortNum());
                map.put("optionContent",questionOption.getOptionContent());
                map.put("isAnswer",questionOption.getIsAnswer());
                map.put("answerSort",questionOption.getAnswerSort());
                mapList.add(map);
            }
            courseExercisesResult.setOptions(mapList);
        }
        return list;
    }

    /**
     * 新增课后练习结果汇总
     * 
     * @param stuCourseExercisesResult 课后练习结果汇总
     * @return 结果
     */
    @Override
    public int insertStuCourseExercisesResult(StuCourseExercisesResult stuCourseExercisesResult)
    {
        return stuCourseExercisesResultMapper.insertStuCourseExercisesResult(stuCourseExercisesResult);
    }

    @Override
    public int insertStuCourseExercisesResultList(List<StuCourseExercisesResult> stuCourseExercisesResultList) {
        return stuCourseExercisesResultMapper.insertStuCourseExercisesResultList(stuCourseExercisesResultList);
    }

    /**
     * 修改课后练习结果汇总
     * 
     * @param stuCourseExercisesResult 课后练习结果汇总
     * @return 结果
     */
    @Override
    public int updateStuCourseExercisesResult(StuCourseExercisesResult stuCourseExercisesResult)
    {
        return stuCourseExercisesResultMapper.updateStuCourseExercisesResult(stuCourseExercisesResult);
    }

    /**
     * 批量删除课后练习结果汇总
     * 
     * @param ids 需要删除的课后练习结果汇总ID
     * @return 结果
     */
    @Override
    public int deleteStuCourseExercisesResultByIds(Long[] ids)
    {
        return stuCourseExercisesResultMapper.deleteStuCourseExercisesResultByIds(ids);
    }

    /**
     * 删除课后练习结果汇总信息
     * 
     * @param id 课后练习结果汇总ID
     * @return 结果
     */
    @Override
    public int deleteStuCourseExercisesResultById(Long id)
    {
        return stuCourseExercisesResultMapper.deleteStuCourseExercisesResultById(id);
    }

    @Override
    public IPage<CompositionPage> page4CompositionByMemberId(Integer pageNo, Integer pageSize, String memberId) {
        return this.baseMapper.page4CompositionByMemberId(new Page<>(pageNo, pageSize), memberId);
    }
}
